<?php
//菜单，接口权限业务模块
class UploadService extends TP_Service {
    private $action_whiteList = array("__construct", "doTransaction", "get_instance");
    public function __construct() {
        parent::__construct();
    }




    function uploadSign($base64_image_content, $name, $file = false) {
        // $director = "upload/" . $name . "/";
        $director = "upload/";
        //匹配出图片的格式
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
            $type = $result[2];
            // $new_file = $director . "" . date('Ymd', time()) . "/";
            $new_file = $director;
            if (!file_exists($new_file)) {
                mkdir($new_file, 0777);
            }
            if (!$file) {
                $f_name = time() . getRandStr(8) . "." . $type . "";
                $new_file = $new_file . $f_name;
            } else {
                $new_file = $new_file . $file . "." . $type . "";
            }


            if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))) {
                // return retArray($file . "." . $type, true, $new_file);
                return $f_name;
            }
        }
        return retArray("图片上传失败.", false);
    }




    /**
     * [buildOrdeNo]
     * @Name     buildOrdeNo
     * @Desc     生成16位订单编号
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author   xy
     * @DateTime 2020-12-25T12:47:19+0800
     * @return   [type]                   [description]
     */
    public function buildOrdeNo() {
        $trade_no = substr(date('Y'), 3, 1) . time() . rand(100, 999) . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 5, 13), 1))), 9, 2);
        return $trade_no;
    }

    /**
     * [upload_img]
     * @Name     upload_img
     * @Desc     上传图片
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author   xy
     * @DateTime 2020-12-25T11:10:47+0800
     * @param    string                   $Folder    [文件夹名]
     * @param    string                   $Imgs    [图片]
     * @return   [type]                              [文件地址]
     */
    public function upload_img($Folder = '', $allowSize) {
        $upload_dir = "upload/" . $Folder . "/"; //判断目录是否存在,不存在则创建目录
        if (!file_exists($upload_dir)) {
            mkdir($upload_dir, 0777);
        }
        $output_dir = $upload_dir . date('Ymd', time()) . "/"; //判断子目录是否存在,不存在则创建
        if (!file_exists($output_dir)) {
            mkdir($output_dir, 0777);
        }

        //上传图片
        if (isset($_FILES["img"])) {
            if (!is_array($_FILES["img"]['name'])) { //单图上传
                $error = $_FILES["img"]["error"];
                $type = explode('.', $_FILES["img"]['name']);
                $document_name = $this->buildOrdeNo();
                $fileName = $document_name . '.' . $type[count($type) - 1];
                //确定icon
                $icon = 'other.png';
                foreach ((new DocumentType)->getAll() as $k => $v) {
                    if (in_array($type[count($type) - 1], $v[TPEnum::Caption])) {
                        $icon = $v[TPEnum::Append1];
                        break;
                    }
                }
                if ($icon != DocumentType::Img[TPEnum::Append1]) {
                    exit(retJson(ErrCode::ErrCode_10008));
                }
                if ($_FILES["img"]["size"] > $allowSize) { //20971520
                    exit(retJson(ErrCode::ErrCode_10009));
                }
                move_uploaded_file($_FILES["img"]["tmp_name"], $output_dir . $fileName);
                $path = $output_dir . $fileName;
                return array('path' => $path, 'icon' => $icon);
            } else {

                //多张图上传
                $path = array();
                $icon = array();
                foreach ($_FILES["img"]['name'] as $k1 => $v1) {
                    $error = $_FILES["img"]["error"][$k1];
                    $type = explode('.', $_FILES["img"]['name'][$k1]);
                    $document_name = $this->buildOrdeNo();
                    $fileName = $document_name . '.' . $type[count($type) - 1];
                    //确定icon
                    $icon_item = 'other.png';
                    foreach ((new DocumentType)->getAll() as $k => $v) {
                        if (in_array($type[count($type) - 1], $v[TPEnum::Caption])) {
                            $icon_item = $v[TPEnum::Append1];
                            $icon[] = $icon_item;
                            break;
                        }
                    }
                    if ($icon_item != DocumentType::Img[TPEnum::Append1]) {
                        exit(retJson(ErrCode::ErrCode_10008));
                    }
                    if ($_FILES["img"]["size"][$k1] > $allowSize) {
                        exit(retJson(ErrCode::ErrCode_10009));
                    }
                    move_uploaded_file($_FILES["img"]["tmp_name"][$k1], $output_dir . $fileName);
                    $path[] = $output_dir . $fileName;
                }
                return array('path' => $path, 'icon' => $icon);
            }
        } else {
            exit(retJson(ErrCode::ErrCode_10006));
        }
    }


    /**
     * [UploadFile]
     * @Name     UploadFile
     * @Desc     文件上传
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author   xy
     * @DateTime 2021-01-25T09:15:54+0800
     * @param    string                   $Folder   [文件夹名称]
     * @param    boolean                  $is_excel [是否为excel文件]
     */
    public function  UploadFile($Folder = '', $is_excel = false) {
        $upload_dir = "upload/" . $Folder . "/"; //判断目录是否存在,不存在则创建目录
        if (!file_exists($upload_dir)) {
            mkdir($upload_dir, 0777);
        }
        $output_dir = $upload_dir . date('Ymd', time()) . "/"; //判断子目录是否存在,不存在则创建
        if (!file_exists($output_dir)) {
            mkdir($output_dir, 0777);
        }

        //上传文件
        if (isset($_FILES["file"])) {
            if (!is_array($_FILES["file"]['name'])) { //单文上传
                $error = $_FILES["file"]["error"];
                $type = explode('.', $_FILES["file"]['name']);
                $document_name = $this->buildOrdeNo();
                $fileName = $document_name . '.' . $type[count($type) - 1];
                //确定icon
                $icon = 'other.png';
                foreach ((new DocumentType)->getAll() as $k => $v) {
                    if (in_array($type[count($type) - 1], $v[TPEnum::Caption])) {
                        $icon = $v[TPEnum::Append1];
                        break;
                    }
                }

                if ($is_excel == true) {
                    if ($icon != DocumentType::Excel[TPEnum::Append1]) {
                        exit(retJson(ErrCode::ErrCode_10010));
                    }
                }
                move_uploaded_file($_FILES["file"]["tmp_name"], $output_dir . $fileName);
                $path = $output_dir . $fileName;
                return array('path' => $path, 'icon' => $icon);
            } else {

                //多文件上传
                $path = array();
                $icon = array();
                foreach ($_FILES["file"]['name'] as $k1 => $v1) {
                    $error = $_FILES["file"]["error"][$k1];
                    $type = explode('.', $_FILES["file"]['name'][$k1]);
                    $document_name = $this->buildOrdeNo();
                    $fileName = $document_name . '.' . $type[count($type) - 1];
                    //确定icon
                    $icon_item = 'other.png';
                    foreach ((new DocumentType)->getAll() as $k => $v) {
                        if (in_array($type[count($type) - 1], $v[TPEnum::Caption])) {
                            $icon_item = $v[TPEnum::Append1];
                            $icon[] = $icon_item;
                            break;
                        }
                    }

                    if ($is_excel == true) {
                        if ($icon_item != DocumentType::Excel[TPEnum::Append1]) {
                            exit(retJson(ErrCode::ErrCode_10010));
                        }
                    }

                    move_uploaded_file($_FILES["file"]["tmp_name"][$k1], $output_dir . $fileName);
                    $path[] = $output_dir . $fileName;
                }
                return array('path' => $path, 'icon' => $icon);
            }
        } else {
            exit(retJson(ErrCode::ErrCode_10007));
        }
    }



    /**
     * [UploadFile]
     * @Name     UploadFile
     * @Desc     文件上传
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author   xy
     * @DateTime 2021-01-25T09:15:54+0800
     * @param    string                   $Folder   [文件夹名称]
     * @param    boolean                  $is_excel [是否为excel文件]
     */
    public function  UploadFile1($Folder = '', $card_id, $is_excel = false, $param) {

        if (!isset($param['name'], $param['icard_number'])) {
            exit(retJson(ErrCode::ErrCode_10002));
        }


        $upload_dir = "upload/" . $Folder . "/"; //判断目录是否存在,不存在则创建目录
        if (!file_exists($upload_dir)) {
            mkdir($upload_dir, 0777);
        }
        $output_dir = $upload_dir . $card_id . "/"; //判断子目录是否存在,不存在则创建
        if (!file_exists($output_dir)) {
            mkdir($output_dir, 0777);
        }

        //上传文件
        if (isset($_FILES["file"])) {

            if ($_FILES["file"]['size'] >= 26214400) {

                exit(retJson(ErrCode::ErrCode_10901));
            }
            if (!is_array($_FILES["file"]['name'])) { //单文上传
                $error = $_FILES["file"]["error"];
                $type = explode('.', $_FILES["file"]['name']);

                //  var_dump($type['1']);
                if ($type['1'] != 'zip' && $type['1'] != "rar") {
                    exit(retJson(ErrCode::ErrCode_10902));
                }
                //  $document_name=$this->buildOrdeNo();
                $fileName = $param['name'] . '-' . $param['icard_number'] . '.' . $type['1'];
                //确定icon
                $icon = 'other.png';
                foreach ((new DocumentType)->getAll() as $k => $v) {
                    if (in_array($type[count($type) - 1], $v[TPEnum::Caption])) {
                        $icon = $v[TPEnum::Append1];
                        break;
                    }
                }

                if ($is_excel == true) {
                    if ($icon != DocumentType::Excel[TPEnum::Append1]) {
                        exit(retJson(ErrCode::ErrCode_10010));
                    }
                }
                move_uploaded_file($_FILES["file"]["tmp_name"], $output_dir . $fileName);
                $path = $output_dir . $fileName;
                return array('path' => $path, 'icon' => $icon);
            } else {

                //多文件上传
                $path = array();
                $icon = array();
                foreach ($_FILES["file"]['name'] as $k1 => $v1) {
                    $error = $_FILES["file"]["error"][$k1];
                    $type = explode('.', $_FILES["file"]['name'][$k1]);
                    $document_name = $this->buildOrdeNo();
                    $fileName = $document_name . '.' . $type[count($type) - 1];
                    //确定icon
                    $icon_item = 'other.png';
                    foreach ((new DocumentType)->getAll() as $k => $v) {
                        if (in_array($type[count($type) - 1], $v[TPEnum::Caption])) {
                            $icon_item = $v[TPEnum::Append1];
                            $icon[] = $icon_item;
                            break;
                        }
                    }

                    if ($is_excel == true) {
                        if ($icon_item != DocumentType::Excel[TPEnum::Append1]) {
                            exit(retJson(ErrCode::ErrCode_10010));
                        }
                    }

                    move_uploaded_file($_FILES["file"]["tmp_name"][$k1], $output_dir . $fileName);
                    $path[] = $output_dir . $fileName;
                }
                return array('path' => $path, 'icon' => $icon);
            }
        } else {
            exit(retJson(ErrCode::ErrCode_10007));
        }
    }
}
